import Vue from 'vue'
import VueRouter from 'vue-router'

import LoginView from '../views/LoginView.vue'
const MainView = () => import('../views/MainView.vue')
const NewQues = () => import('../views/NewQues.vue')
const Manage = () => import('../views/Manage.vue')
const Box = () => import('../views/Box.vue')
const TeacherInfo = () => import('../views/TeacherInfo.vue')
const StudentInfo = () => import('../views/StudentInfo.vue')
const EditQues = () => import('../views/EditQues.vue')
const Password = () => import('../views/Password.vue')
const Admin = () => import('../views/Admin.vue')
const Analyses = () => import('../views/Analyses.vue')
const Publish = () => import('../views/Publish.vue')
const Step1 = () => import('../views/publishStep/Step1.vue')
const Step2 = () => import('../views/publishStep/Step2.vue')
const Step3 = () => import('../views/publishStep/Step3.vue')
const Step4 = () => import('../views/publishStep/Step4.vue')
const EmitFinish = () => import('../views/publishStep/EmitFinish.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/main',
    name: 'main',
    component: MainView,
    redirect: '/main/new',
    children: [
      {
        path: 'new',
        component: NewQues
      },
      {
        path: 'manage',
        component: Manage,
      },
      {
        path: 'box',
        component: Box
      },
      {
        path: 'teacher',
        component: TeacherInfo
      },
      {
        path: 'student',
        component: StudentInfo
      },
      {
        path: 'edit',
        component: EditQues,
        name: 'edit'
      },
      {
        path: 'analyses',
        component: Analyses,
        name: 'analyses'
      },
      {
        path: 'publish',
        component: Publish,
        name: 'publish',
        redirect: 'publish/step1',
        children: [
          {
            path: 'step1',
            component: Step1
          },
          {
            path: 'step2',
            component: Step2
          },
          {
            path: 'step3',
            component: Step3
          },
          {
            path: 'step4',
            component: Step4
          },
          {
            path: 'emitFinish',
            component: EmitFinish
          }
        ]
      },
      {
        path: 'password',
        component: Password
      },
      {
        path: 'admin',
        component: Admin
      }
    ]
  }
  // {
  // path: '/about',
  // name: 'about',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})
let key = window.btoa('isLogin')
router.beforeEach((to, from, next) => {
  console.log(to);
  console.log(from);
  if (to.name === 'login') {
    next()
  } else if (sessionStorage.getItem(key) == 1) {
    next()
  } else {
    console.log(key);
    next('/')
  }
})

export default router
